package cn.chenxhcloud.myvv;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.voovan.db.CallType;
import org.voovan.db.JdbcOperate;
import org.voovan.tools.TEnv;
import org.voovan.tools.TObject;
import org.voovan.tools.TProperties;
import org.voovan.tools.log.Logger;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

public class DbOperateDemo {
	private static DruidDataSource getDataSource() {
		DruidDataSource dataSource = null;
        try {
            String druidpath = TEnv.getClassLocation(DbOperateDemo.class)+"/conf/db.properties";
            Logger.simple(druidpath);
            Properties druidProperites = TProperties.getProperties(new File(druidpath));
            dataSource = TObject.cast(DruidDataSourceFactory.createDataSource(druidProperites));
            dataSource.init();
            Logger.info("Database connection pool init finished");
        } catch (Exception e) {
            Logger.error(e);
        }
        return dataSource;
	}
	
	public static void main(String[] args) throws Exception {
		DataSource dataSource = getDataSource();
		JdbcOperate jOperate = new JdbcOperate(dataSource);
		Map<String, Object> data = jOperate.queryMap("select 100011 as bb");
		Logger.simple(data.get("bb"));
		
		Map<String,Long> params = new HashMap<String,Long>();
		params.put("id", 12356321L);
		Map<String,Object> retData = jOperate.queryObject("select * from tb_threads t where t.id = ::1  limit 0,10",Map.class,params.get("id"));
		Logger.simple(retData);
		
		params.put("aaa", 120L);
		List<Object> datas = jOperate.call("{call proc1(::1)}",  CallType.values() , params.get("aaa"));
		Logger.simple(datas.get(0));
		
	}
	
}
